import $http from '../utils/http.js'
import download from '../utils/download.js'
import { Notification } from 'atelement'
const getdata = '/operatelog/getdata'
const handleCurrentChange = '/operatelog/handleCurrentChange'
const handleSizeChange = '/operatelog/handleSizeChange'
const handleIconClick = '/operatelog/handleIconClick'
const order = '/operatelog/order'
const deadline = '/operatelog/deadline'
const updata = '/operatelog/updata'
const changetime = '/operatelog/changetime'
const filterChange = '/operatelog/filterChange'
const userPersonName = '/operatelog/userpersonname'
const exportfile = '/operatelog/exportfile'
const changeLoading = '/operatelog/changeLoading'

export default {
  state: {
    deadline: [],
    line: [],
    user_name: [],
    person_name: [],
    fullscreenLoading: false,
    tableBox: {
      tableData: {
        'pagination': {
          'current': 1, // 当前页码
          'pageSize': 20// 当前页显示的数据条数
        },
        'search': {
          'value': '' // 需要模糊搜索的值（搜索框）
        },
        'filters': {
          'buser_name': [], // 账号
          'cperson_name': [], // 用户名
          'aoperate_type': [] // 类型
        },
        'sorter': {
          'field': 'aoperate_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        },
        'timer': {
          'time': {
            'field': 'aoperate_time',
            'startTime': '',
            'endTime': ''
          }
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    }
  },
  actions: {
    // 改变页数
    [handleCurrentChange] ({ state, commit }, page) {
      state.tableBox.tableData.pagination.current = page
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(handleCurrentChange, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '换页失败',
            message: msg
          })
        }
      })
    },
    // 每页多少条
    [handleSizeChange] ({ state, commit }, pageSizes) {
      state.tableBox.tableData.pagination.pageSize = pageSizes
      state.tableBox.tableData.pagination.current = 1
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(handleSizeChange, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取失败',
            message: msg
          })
        }
      })
    },
    // 搜索
    [handleIconClick] ({ state, commit }, Value, currentPagee) {
      state.tableBox.tableData.search.value = Value
      state.tableBox.tableData.pagination.current = currentPagee
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(handleIconClick, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '查询失败',
            message: msg
          })
        }
      })
    },
    // 时间排序
    [order] ({ state, commit }, { key, value }) {
      state.tableBox.tableData.sorter.order = value
      state.tableBox.tableData.sorter.field = key
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat)
        .then(({ status, msg, data } = {}) => {
          if (status === 'success') {
            commit(order, data)
          } else if (status === 'fail') {
            Notification.error({
              title: '排序失败',
              message: msg
            })
          }
        })
    },
    // 选择时间段
    [changetime] ({ state, commit }, { key, startTime, endTime }) {
      state.tableBox.tableData.timer.time.field = key
      state.tableBox.tableData.timer.time.startTime = startTime
      state.tableBox.tableData.timer.time.endTime = endTime
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat)
        .then(({ status, msg, data } = {}) => {
          if (status === 'success') {
            commit(changetime, data)
          } else if (status === 'fail') {
            Notification.error({
              title: '选择时间失败',
              message: msg
            })
          }
        })
    },
    // 获取数据
    [getdata] ({ state, commit }) {
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(getdata, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取数据失败',
            message: msg
          })
        }
      })
    },
    // 筛选数据
    [filterChange] ({ state, commit }, obj) {
      for (let [key, val] of Object.entries(obj)) {
        state.tableBox.tableData.filters[key] = val
      }
      state.tableBox.tableData.pagination.current = 1
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      $http.option('/api/business/operatelog', sat
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(filterChange, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '筛选失败',
            message: msg
          })
        }
      })
    },
    // 导出文件
    [exportfile] ({ state }, num) {
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        },
        timer: {
          time: {
            'field': state.tableBox.tableData.timer.time.field,
            'startTime': state.tableBox.tableData.timer.time.startTime,
            'endTime': state.tableBox.tableData.timer.time.endTime
          }
        }
      }
      sat.filters = {}
      for (let [key, val] of Object.entries(state.tableBox.tableData.filters)) {
        if (val.length > 0) {
          sat.filters[key] = val
        }
      }
      if (num === 1) {
        download.option('/api/business/operatelog/exportexcel', sat)
      } else {
        download.option('/api/business/operatelog/exportcsv', sat)
      }
    },
    // 获取账号,用户名
    [userPersonName] ({ state, commit }) {
      $http.post('/api/business/operatelog/filter'
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(userPersonName, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取账号,用户名失败',
            message: msg
          })
        }
      })
    },
    // 获取最后期限
    [deadline] ({ state, commit }) {
      $http.get('/api/business/operatelog/deadline'
      ).then(({ status, msg, data } = {}) => {
        if (status === 'success') {
          commit(deadline, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取期限失败',
            message: msg
          })
        }
      })
    },
    // 更新设置期限
    [updata] ({ state }, { data, func }) {
      $http.post('/api/business/operatelog/updatadeadline', data
      ).then(({ status, msg } = {}) => {
        if (status === 'success') {
          func()
        } else if (status === 'timeout') {
          Notification.error({
            title: '不具备操作权限',
            message: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: msg,
            message: msg
          })
        }
      })
    }
  },
  mutations: {
    [changeLoading] (state) {
      state.fullscreenLoading = !state.fullscreenLoading
    },
    // 获取数据
    [getdata] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 改变页数
    [handleCurrentChange] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 每页多少条
    [handleSizeChange] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 搜索
    [handleIconClick] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 时间排序
    [order] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 选择时间段
    [changetime] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
    },
    // 筛选数据
    [filterChange] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
      state.tableBox = {
        'table': data.dataSource,
        'total': data.total,
        'tableData': state.tableBox.tableData
      }
    },
    // 获取账号,用户名
    [userPersonName] (state, data) {
      state.user_name = data.user_name
      state.person_name = data.person_name
    },
    // 获取最后期限
    [deadline] (state, data) {
      state.line = data.deadline[0]['deadline']
      if (state.line === '6 mouth') {
        state.deadline = '6个月'
      } else if (state.line === '1 year') {
        state.deadline = '1年'
      } else if (state.line === '2 year') {
        state.deadline = '2年'
      } else {
        state.deadline = '3年'
      }
    }
  }
}
